<template>
  <div class="goods">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCateBtn">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 主要内容区 -->
      <tree-table
        :columns="columns"
        :expand-type="false"
        :show-index="true"
        :selection-type="false"
        :data="cateList"
        index-text="#"
        border
      >
        <!-- 是否有效 -->
        <template v-slot:isEf="scope">
          <i
            class="el-icon-success"
            style="color:red;font-size:14px;"
            v-show="scope.row.cat_deleted"
          ></i>
          <i
            class="el-icon-success"
            style="color:green;font-size:14px;"
            v-show="!scope.row.cat_deleted"
          ></i>
        </template>
        <!-- 排序 -->
        <template v-slot:sort="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template v-slot:pot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="editCate(scope.row.cat_id)"
            icon="el-icon-edit"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="delRole(scope.row.cat_id)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <div class="block">
        <el-pagination
          :page-sizes="[5,10,15,20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-size="5"
          @current-change="changePage"
          @size-change="changeSizePage"
          :current-page.sync="currentPage"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 添加分类弹窗 -->
    <el-dialog :title="cateTitle" :visible.sync="dialogCateVisible" width="50%" class="catedialog">
      <el-form :model="cateForm" :rules="cateRules" ref="cateForm" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateForm.cat_name" :autofocus="true"></el-input>
        </el-form-item>
        <el-form-item v-show="cateTitle === '添加分类'" label="父级分类">
          <el-cascader
            @change="cateChange"
            :options="cateSelectList"
            :props="cateProps"
            v-model="catKeys"
            :clearable="true"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogCateVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 分类列表
      cateList: [],
      // 添加分类中的下拉列表
      cateSelectList: [],
      cateTitle: '',
      catKeys: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 类型，选用模板
          type: 'template',
          // 模板名称:是否有效
          template: 'isEf'
        },
        {
          label: '排序',
          type: 'template',
          // 排序
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'pot',
          width: '200px'
        }
      ],
      // 参数列表
      paramsInfo: {
        type: 3, // 获取三层分类列表
        pagenum: 1, // 默认第一页
        pagesize: 5 // 每页显示条数
      },
      // 总页数
      total: 0,
      currentPage: 1,
      cateId: 0,
      dialogCateVisible: false, // 控制添加分类弹窗
      // 添加分类
      cateForm: {
        cat_name: '', // 分类名称
        cat_pid: 0, // 分类id
        cat_level: 0 // 分类等级
      },
      // 验证规则
      cateRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      // treetable prop配置
      cateProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: 'true'
      }
    }
  },
  created() {
    // this.getCateList()
    this.$store.dispatch('getCates')
  },

  methods: {
    // 获取商品分类数据
    async getCateList() {
      const { data: res } = await this.$axios.get('categories', {
        params: this.paramsInfo
      })
      if (res.meta.status !== 200) return this.$message.error('商品分类获取失败')
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 翻页事件
    changePage(page) {
      this.paramsInfo.pagenum = page
      this.getCateList()
    },
    // 设置每页显示条数
    changeSizePage(size) {
      this.paramsInfo.pagesize = size
      this.getCateList()
    },
    // 添加分类弹窗
    async addCateBtn() {
      this.cateTitle = '添加分类'
      const { data: res } = await this.$axios.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.cateSelectList = res.data
      this.dialogCateVisible = true
    },
    // 添加/编辑分类
    addCate() {
      this.$refs.cateForm.validate(async valid => {
        if (!valid) return this.$message.error('请输入分类名')
        if (this.cateTitle === '添加分类') {
          const { data: res } = await this.$axios.post('categories', this.cateForm)
          if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
          this.$message.success('添加成功')
          console.log('添加')

          return
        } else {
          // 编辑分类
          const { data: res } = await this.$axios.put('categories/' + this.cateId, {
            cat_name: this.cateForm.cat_name
          })
          if (res.meta.status !== 200) return this.$message.error('修改失败')
          this.getCateList()
          this.$message.success('修改成功')
        }
      })

      this.getCateList()
      this.dialogCateVisible = false
    },
    cateChange() {
      if (this.catKeys.length > 0) {
        this.cateForm.cat_pid = Array.from(this.catKeys)[this.catKeys.length - 1]
        this.cateForm.cat_level = this.catKeys.length
        console.log(this.cateForm)
        return
      } else {
        this.cateForm.cat_pid = 0
        this.cateForm.cat_level = 0
      }
    },
    async editCate(cateId) {
      this.cateTitle = '编辑分类'
      this.cateId = cateId // 编辑分类id
      const { data: res } = await this.$axios.get('categories/' + this.cateId)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.cateForm.cat_name = res.data.cat_name
      this.dialogCateVisible = true
    },
    async delRole(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$axios.delete('categories/' + id)
          if (res.meta.status !== 200) return this.$message.error('删除失败')
          this.$message.success('删除成功')
          this.getCateList()
        })
        .catch(err => err)

      // const { data: res } = await this.$axios.delete('roles/'+id)
    }
  },

  mounted() {}
}
</script>

<style lang='less' scoped>
.categories {
  .el-cascader {
    width: 100%;
  }
}
</style>
